/**
 * Created by Administrator on 15-1-7.
 * 系统管理使用
 */
require.config({
    baseUrl:PUBLIC + "/Admin/js/lib"
});
define(function(require){
    var $ = require('jquery');
    require('jquery.form');
    require('tips/msgbox');
    window.$ = window.jQuery = $;
    var c=require('../common');
    require('datetimepicker/datetimepicker');
    var dialog = require('bDialog/dialog');
    require('select');
    window.dialog=dialog;

    $(function() {

        //下拉框初始化
        $('#shop_sel').selectbox();

        $('#publish_time,#ext_time').datetimepicker({
            format:'Y-m-d',
            onShow:function( ct ){
                this.setOptions({
                    minDate:new Date()
                })
            },
            closeOnDateSelect:true,
            timepicker:false
        });

        //删除
        $('table .del').off().on('click', function () {
            var id = $(this).data('id');
            if (empty(id)) {
                c.error('请选择操作项！', 2000);
                return false;
            }

            dialog({
                title: '温馨提示', height: 100, icon: 'warning', content: '是否确定删除？', ok: function () {
                    $.get(URL + '/del?id=' + id, '', function (data) {
                        if (data.status == 1) {
                            c.success(data.info, 1000);
                            setTimeout(function () {
                                location.reload();
                            }, 1500);
                            return true;
                        } else {
                            c.error(data.info, 2000);
                            return false;
                        }
                    }, 'json');
                },
                cancel: function () {
                },
                okVal: '确定', cancelVal: '取消'
            });
        });

        //更改状态
        $('table .setPass').off().on('click', function () {
            var t = $(this);
            var id = t.data('id');
            var msg = t.html();
            if (empty(id)) {
                c.error('请选择操作项！', 2000);
                return false;
            }

            dialog({
                title: '温馨提示', height: 100, icon: 'warning', content: '是否' + msg + '该数据?', ok: function () {
                    $.get(URL + '/setStatus?id=' + id, '', function (data) {
                        if (data.status == 1) {
                            c.success(data.info, 1000);
                            setTimeout(function () {
                                location.reload();
                            }, 1500);
                            return true;
                        } else {
                            c.error(data.info, 2000);
                            return false;
                        }
                    }, 'json');
                },
                cancel: function () {
                },
                okVal: '确定', cancelVal: '取消'
            });
        });

        //表单提交
        $('#detail_form').ajaxForm({
            beforeSubmit: checkForm,  // pre-submit callback
            success: complete,  // post-submit callback
            dataType: 'json'
        });

        function checkForm() {
            return true;
        }

        function complete(data) {
            if (data.status == 1) {
                c.success(data.info, 1000);
                if (data.data) {
                    setTimeout(function () {
                        location.href = data.data;
                    }, 1500);
                    return true;
                } else {
                    setTimeout(function () {
                        window.location.reload();
                    }, 1000);
                }
            } else {
                c.error(data.info, 2000);
                return false;
            }
        }
    })
});